import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: ''
  },
  getters: {
    getUser: state => {
      if (!state.user) {
        let userStr = sessionStorage.getItem('user');
        try {
          state.user = JSON.parse(userStr);
        } catch (e) {
          return null;
        }
      }
      return state.user;
    }
  },
  mutations: {
    setUser: (state, user) => {
      state.user = user;
      sessionStorage.setItem('user', JSON.stringify(user));
    }
  },
})
